
// 定时器标识
let touchThrottleId:any = null
let virtaulDom:HTMLElement;
let mainDom:HTMLElement|null = null
let isMoveable = false


const createMoveVirtaulDom=(e)=>{
    mainDom = e.target
    virtaulDom = e.target?.cloneNode(true)
}


const touchStart = (e) => {
    isMoveable = false
    createMoveVirtaulDom(e)
    mainDom?.parentNode?.appendChild(virtaulDom)
}


const touchMove =(e)=>{
    e.preventDefault()
    isMoveable = true
    if(touchThrottleId)   return
    touchThrottleId = setTimeout(()=>{
        virtaulDom?.setAttribute('style',`top:${e.changedTouches[0].clientY}px;left:${e.changedTouches[0].clientX}px;opacity:0.5 !important`)
        clearTimeout(touchThrottleId)
        touchThrottleId = null
    },100)
}

const touchEnd = (e) => {
    clearTimeout(touchThrottleId)
    touchThrottleId = null
    // 如果没有触发移动，执行点击事件，否则执行移动事件
    if(isMoveable){
        e.target?.setAttribute('style',`top:${e.changedTouches[0].clientY}px;left:${e.changedTouches[0].clientX}px;`)
    }
    mainDom?.parentNode?.removeChild(virtaulDom)
}

export {
    touchStart,
    touchMove,
    touchEnd,
}